#!/usr/bin/env python
# coding=utf-8
#-------------------------------------------------------------------------------
# Name:        gjsk_getter.py
# Purpose:     get gjsk url
#
# Author:      vavava
#
# Created:     04/11/2012
# Copyright:   (c) vavava 2012
# Licence:     <your licence> reg_str=r"<embed[^>]*src='(?P<link>)[^']*"
#-------------------------------------------------------------------------------

if __name__ == '__main__':
    """ for file debug"""
    import sys,os
    sys.path.insert(0,os.path.join( os.getcwd(), '..' ))

from vavava import http_client

__author__ = 'vavava'

import   re
from vavava import util

class gjsk(object):

    def __init__(self,debugLevel=0):
        self.http = http_client.HttpClient(debug_level=debugLevel)
        self.fetch_url=r'http://vtwtv.info/home.php?mod=space&uid=1278&do=thread&view=me&type=thread&order=dateline&from=space&page=%s'
        self.page_dic={}
        self.video_list=[]
        self.youtube_url_dic={}

    def login(self):
        cookie_str="""lo4X_2132_sid=a03aMd; expires=Wed, 07-Nov-2012 17:49:20 GMT;
        path=/ lo4X_2132_lastact=1352224160%09forum.php%09viewthread;
        expires=Wed, 07-Nov-2012 17:49:20 GMT; path=/ lo4X_2132_fid192=1352220319;
        expires=Tue, 06-Nov-2012 18:49:20 GMT; path=/ lo4X_2132_visitedfid=192D191D181D124D36D139;
        expires=Thu, 06-Dec-2012 17:49:20 GMT; path=/ lo4X_2132_sid=a03aMd;
        expires=Wed, 07-Nov-2012 17:49:20 GMT; path=/"""
        self.http.cookie_str = cookie_str

    def get_page_dic(self,index):
        self.login()
        page = self.http.Get(self.fetch_url%index)
        page_text=page.decode('utf-8','ignore')
        reg_str="<a[^>]*href=\"(?P<url>[^\"]*)\"[^>]*> (?P<info>[^<]*關鍵時刻[^<]*)<"
        reg=re.compile(reg_str,re.X)
        match=reg.findall(page_text)
        if match:
            for each in match:
                self.page_dic[each[1]]='http://vtwtv.info/'+each[0].replace('&amp;','&')
            return True
        return False

    def get_youtube_url(self,indexes):
        for index in indexes:
            try:
                self.get_page_dic(index)
            except Exception as e:
                print(e)

        for each in self.page_dic:
            try:
                url=self.page_dic.get(each)
                print (url)
                page = self.http.Get(url)
                page_text=page.decode('utf-8','ignore')
                reg_str=r"<param [^\?]*value=[^\?]*/(?P<link>[^\?]*)\?"
                reg=re.compile(reg_str,re.S)
                match=reg.findall(page_text)
                if match:
                    url_format=r'http://www.youtube.com/watch?feature=player_embedded&v=%s'%match[0]
                    self.youtube_url_dic[each]=url_format.replace('&amp;','&')
                else:
                    print("not found:",each)
            except Exception as e:
                print(e)
        return self.youtube_url_dic

if __name__== '__main__':
    import sys
    g=gjsk()
    indexes=["1"]
    argv_dic=util.parse_argvs(sys.argv)
    if argv_dic:
        if argv_dic.get('-pages'):  indexes=argv_dic.get('-pages').split(',')

    if g.get_youtube_url(indexes):
        for each in g.youtube_url_dic:
            print(r'%s       %s'%(each,g.youtube_url_dic.get(each)))
    else:
        print("failed")
